package com.ylkj;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ylkj.bus.device.onoffline.constant.OnOfflineMsgConstant;
import com.ylkj.bus.device.onoffline.model.OnOfflineDeviceEnty;
import com.ylkj.bus.device.report.constant.MsgKeyConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.util.OutputTag;

import java.util.Date;
import java.util.Objects;

/**
 * @Author: wengzx
 * @Date: 2025/6/3
 * @Desc:
 */
@Slf4j
public class Test {

    /**
     * 设备状态: 上线
     */
    public static final String ONLINE = "deviceOnline";

    /**
     * 设备状态: 下线
     */
    public static final String OFFLLINE = "deviceOffline";
    public static void main(String[] args) {
        String s = "{\"notifyType\":\"deviceOnline\",\"deviceIdentifier\":\"0215260001000383\",\"eventTime\":\"1748542768944\",\"deviceId\":\"D1401114184280MnUhg\"}";
        JSONObject body = JSONObject.parseObject(s);
        if(body.isEmpty()){
            log.error("原始数据信息上报的数据为空：{}",s);
            return;
        }
        String notifyType = body.getString(MsgKeyConstant.NOTIFY_TYPE);
        String deviceCode = body.getString(MsgKeyConstant.DEVICE_IDENTIFIER);
        String eventTime = body.getString(MsgKeyConstant.EVENT_TIME);
        long eventTimeLong = Long.parseLong(eventTime);
        if (StrUtil.isEmpty(deviceCode)) {
            log.error("原始数据信息上报的deviceCode为空：{}",s);
            return;
        }
        if(!Objects.equals(notifyType,ONLINE) && !Objects.equals(notifyType,OFFLLINE)){
            log.error("原始数据信息上报的上下线值为空：{}",s);
            return;
        }

        Date now = DateUtil.date();
        OnOfflineDeviceEnty enty = new OnOfflineDeviceEnty();
        if(Objects.equals(notifyType,ONLINE)){
            enty.setRomaOnlineStatus(OnOfflineMsgConstant.DEVICE_ONLINE);
        }else {
            enty.setRomaOnlineStatus(OnOfflineMsgConstant.DEVICE_OFFLINE);
        }
        enty.setLastOnlineStatusTime(DateUtil.date(eventTimeLong));
        enty.setUpdateTime(now);
        enty.setDeviceCode(deviceCode);
        System.out.println(enty);
    }
}


